﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using converter.template;
using converter.template.item;

namespace converter
{
    /// <summary>
    /// Writes a template to a Word document.
    /// </summary>
    public sealed class WordDocumentBuilder
    {
        /// <summary>
        /// Builds a Word document.
        /// </summary>
        /// <param name="template">A template content which goes into the document.</param>
        /// <param name="fileName">A path to a Word document to write the output to.</param>
        public static void Build(Template template, string fileName)
        {
            // Create a new Word document.
            using (WordprocessingDocument package = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
            {
                // Main document part.
                MainDocumentPart mainPart = package.AddMainDocumentPart();

                // The document's content.
                Document document = new Document();
                mainPart.Document = document;

                // The document's body.
                Body body = new Body();
                document.AppendChild(body);
                foreach (TemplateItem item in template.Body)
                    item.Build(body);

                // Section properties.
                SectionProperties sectPr = new SectionProperties();
                body.AppendChild(sectPr);
            }
        }
    }
}
